package com.yugong.controller;

import com.github.pagehelper.PageInfo;
import com.yugong.common.Result;
import com.yugong.entity.vo.request.BuildingRequestVO;
import com.yugong.entity.vo.response.BuildingListResponseVO;
import com.yugong.service.BuildingService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 楼栋控制器类
 *
 * @author yugong
 * @date 2024/2/25
 */
@RestController
@RequestMapping("/api/building")
public class BuildingController {
    @Resource
    private BuildingService buildingService;

    /**
     * 根据楼栋名查询楼栋
     *
     * @param pageNum      当前页
     * @param pageSize     当前页数据总个数
     * @param buildingName 楼栋名
     * @return 楼栋
     */
    @GetMapping("/find-building-page")
    public Result findBuildingPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "5") Integer pageSize,
                                   @RequestParam String buildingName) {
        PageInfo<BuildingListResponseVO> buildingListResponseVOPageInfo
                = buildingService.findBuilding(pageNum, pageSize, buildingName);
        return Result.success(buildingListResponseVOPageInfo);
    }

    /**
     * 查询楼栋
     *
     * @return 楼栋
     */
    @GetMapping("/find-building")
    public Result findBuilding() {
        List<BuildingListResponseVO> buildingListResponseVOList = buildingService.findBuilding();
        return Result.success(buildingListResponseVOList);
    }

    /**
     * 新增楼栋
     *
     * @param buildingRequestVO 楼栋请求实体类
     * @return 响应码
     */
    @PostMapping("/add-building")
    public Result addBuilding(@RequestBody BuildingRequestVO buildingRequestVO) {
        int addFlag = buildingService.addBuilding(buildingRequestVO);
        return Result.success(addFlag);
    }

    /**
     * 更新楼栋
     *
     * @param buildingRequestVO 楼栋请求实体类
     * @return 响应码
     */
    @PutMapping("/update-building")
    public Result updateBuilding(@RequestBody BuildingRequestVO buildingRequestVO) {
        int updateFlag = buildingService.updateBuilding(buildingRequestVO);
        return Result.success(updateFlag);
    }

    /**
     * 删除楼栋
     *
     * @param buildingNo 楼号
     * @return 响应码
     */
    @DeleteMapping("/delete-building/{buildingNo}")
    public Result deleteBuilding(@PathVariable String buildingNo) {
        int deleteFlag = buildingService.deleteBuilding(buildingNo);
        return Result.success(deleteFlag);
    }
}
